package io.wongxd.demo.page;

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import io.wongxd.demo.AppBtn
import io.wongxd.demo.AppLogger
import io.wongxd.solution.baseView.BaseComposeToolBarFgt
import io.wongxd.solution.log.pprint.pprint
import io.wongxd.solution.log.xlog.FLog
import io.wongxd.solution.log.xlog.fDebug
import io.wongxd.solution.log.xlog.flogD
import io.wongxd.solution.log.xlog.flogE
import io.wongxd.solution.log.xlog.flogI
import io.wongxd.solution.log.xlog.flogV
import io.wongxd.solution.log.xlog.flogW
import kotlin.concurrent.thread
import kotlin.time.ExperimentalTime
import kotlin.time.measureTime

class FgtLog : BaseComposeToolBarFgt() {

    data class Name(val first: String, val last: String)
    data class Person(val name: Name, val age: Int)
    val p = Person(Name("姐姐", "大"), 42)

    private fun log() {
        // 打开控制台日志
        FLog.setConsoleLogEnabled(true)

        flogV<AppLogger> { "Verbose" }
        flogD<AppLogger> { "Debug" }
        flogI<AppLogger> { "Info" }
        flogW<AppLogger> { "Warning" }
        flogE<AppLogger> { "Error" }

        thread {
            flogW<AppLogger> { "in thread" }
        }

        // 打印控制台日志，不会写入到文件中，tag：DebugLogger
        fDebug { "console debug log" }


        flogD<AppLogger> {
            p
        }

    }

    /**
     * 日志性能测试，需要关闭控制台日志后测试
     */
    @OptIn(ExperimentalTime::class)
    private fun testPerformance(logLength: Int = 500, repeat: Int = 1_0000) {
        // 关闭控制台日志
        FLog.setConsoleLogEnabled(false)
        val log = "1".repeat(logLength)
        measureTime {
            repeat(repeat) {
                flogI<AppLogger> { log }
            }
        }.let {
            Log.i(FgtLog::class.java.simpleName, "time:${it.inWholeMilliseconds}")
        }
    }

    override fun lazyInit() {

    }

    @Composable
    override fun Render() {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(Color.White),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            AppBtn(text = "日志") {
                log()
            }

            AppBtn(text = "性能") {
                testPerformance()
            }
        }
    }

    @Preview(showBackground = true, backgroundColor = 0xffffffff)
    @Composable
    fun RenderPreView() {

    }
}
